package top.csjss.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.csjss.hospital.entity.Pharmacy;
import top.csjss.hospital.entity.PharmacyExample;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.exception.HospitalException;
import top.csjss.hospital.mapper.PharmacyMapper;
import top.csjss.hospital.service.PharmacyService;

import java.util.List;

/**
 * @Author ztf
 * @Date 15:16 2021/10/21
 **/
@Service
public class PharmacyServiceImpl implements PharmacyService {
    private Logger log = LoggerFactory.getLogger(PharmacyServiceImpl.class);


    @Autowired
    private PharmacyMapper pharmacyMapper;

    @Override
    public PageInfo<Pharmacy> selectDrugs(String drugname, Integer page, Integer limit) {
        //开始分页
        PageHelper.startPage(page, limit);
        //查询
        List<Pharmacy> pharmacies = pharmacyMapper.selectDrug(drugname);
        //封装
        PageInfo<Pharmacy> pageInfo = new PageInfo<>(pharmacies);
        //返回
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addNumByName(Integer drugNum, String drugName) {
        int i = pharmacyMapper.updateNumByName(drugName, drugNum);
        if (i != 1) {
            log.info("【仓库药品信息更新】  仓库药品信息更新失败");
            throw new HospitalException(HospitalExceptionEnums.PHARMACY_UPDATE_NUM_ERROR);
        }
    }
}
